package
{
	import flash.display.MovieClip;

	public class ThwompObject
	{
		public var name:String;
		public var pos:Vector2;
		public var anchor:Vector2;
		public var fallgoal:Vector2;
		public var goal:Vector2;
		public var i:Number;
		public var j:Number;
		public var mini:Number;
		public var minj:Number;
		public var maxi:Number;
		public var maxj:Number;
		public var xw:Number;
		public var yw:Number;
		public var movedir:Number;
		public var fallspeed:Number;
		public var raisespeed:Number;
		public var speed:Number;
		public var playerWasStanding:Boolean;
		public var isMoving:Boolean;
		public var dirEnum:Number;
		public var dir:Vector2;
		public var mc:MovieClip;

		public function ThwompObject()
		{
			this.name = "thwump";
			this.pos = new Vector2(141, 14);
			this.anchor = new Vector2(91, 82);
			this.fallgoal = new Vector2(98, 74);
			this.goal = this.fallgoal;
			this.i = 6;
			this.j = 7;
			this.mini = 2;
			this.minj = 5;
			this.maxi = 8;
			this.maxj = 3;
			this.xw = Main.getInstance().tiles.xw * 0.75;
			this.yw = Main.getInstance().tiles.xw * 0.75;
			this.movedir = 1;
			this.fallspeed = Main.getInstance().tiles.xw * 0.357142857142857;
			this.raisespeed = Main.getInstance().tiles.xw * 0.142857142857143;
			this.speed = this.fallspeed;
			this.playerWasStanding = false;
			this.isMoving = false;
			this.dirEnum = DroneObject.AI_DIR_U;
			this.dir = new Vector2(1, 0);
			Main.getInstance().objects.Register(this);
			this.mc = Main.getInstance().gfx.CreateSprite("debugThwompMC", NinjaGraphicsSystem.LAYER_OBJECTS);
			this.mc.visible = false;
		}

		public function Destruct():void
		{
			Main.getInstance().gfx.DestroyMC(this.mc);
			this.mc = null;
		}

		public var cell:TileMapCell;
		public var Update:Function;

		public function Init(params):void
		{
			if (params.length == 3)
			{
				this.pos.x = params[0];
				this.pos.y = params[1];
				this.anchor.x = this.pos.x;
				this.anchor.y = this.pos.y;
				Main.getInstance().objects.AddToGrid(this);
				Main.getInstance().objects.StartUpdate(this);
				Main.getInstance().objects.Moved(this);
				this.i = this.cell.i;
				this.j = this.cell.j;
				var dir = params[2];
				var wid = 0;
				this.dirEnum = dir;
				if (dir == DroneObject.AI_DIR_U)
				{
					this.dir.x = 0;
					this.dir.y = -1;
					var _local3 = this.pos.x;
					var ty = this.pos.y;
					var _local2 = this.cell.nU;
					while (_local2.ID == TileMapCell.TID_EMPTY)
					{
						ty = ty - (2 * this.cell.yw);
						_local2 = _local2.nU;
					}
					ty = ty - this.yw;
					ty = ty - (this.pos.y - this.cell.pos.y);
					this.mc.rotation = 180;
				}
				else if (dir == DroneObject.AI_DIR_D)
				{
					this.dir.x = 0;
					this.dir.y = 1;
					var _local3 = this.pos.x;
					var ty = this.pos.y;
					var _local2 = this.cell.nD;
					while (_local2.ID == TileMapCell.TID_EMPTY)
					{
						ty = ty + (2 * this.cell.yw);
						_local2 = _local2.nD;
					}
					ty = ty + this.yw;
					ty = ty - (this.pos.y - this.cell.pos.y);
					this.mc.rotation = 0;
				}
				else if (dir == DroneObject.AI_DIR_L)
				{
					this.dir.x = -1;
					this.dir.y = 0;
					var _local3 = this.pos.x;
					var ty = this.pos.y;
					var _local2 = this.cell.nL;
					while (_local2.ID == TileMapCell.TID_EMPTY)
					{
						_local3 = _local3 - (2 * this.cell.xw);
						_local2 = _local2.nL;
					}
					_local3 = _local3 - this.xw;
					_local3 = _local3 - (this.pos.x - this.cell.pos.x);
					this.mc.rotation = 90;
				}
				else if (dir == DroneObject.AI_DIR_R)
				{
					this.dir.x = 1;
					this.dir.y = 0;
					var _local3 = this.pos.x;
					var ty = this.pos.y;
					var _local2 = this.cell.nR;
					while (_local2.ID == TileMapCell.TID_EMPTY)
					{
						_local3 = _local3 + (2 * this.cell.xw);
						_local2 = _local2.nR;
					}
					_local3 = _local3 + this.xw;
					_local3 = _local3 - (this.pos.x - this.cell.pos.x);
					this.mc.rotation = -90;
				}
				this.fallgoal.x = _local3;
				this.fallgoal.y = ty;
				this.goal = this.fallgoal;
				this.i = this.cell.i;
				this.j = this.cell.j;
				this.mini = this.cell.i;
				this.minj = this.cell.j;
				var temptile = Main.getInstance().tiles.GetTile_S(_local3, ty);
				this.maxi = temptile.i;
				this.maxj = temptile.j;
				if (this.dir.x < 0)
				{
					var temp = this.mini;
					this.mini = this.maxi;
					this.maxi = temp;
				}
				if (this.dir.y < 0)
				{
					var temp = this.minj;
					this.minj = this.maxj;
					this.maxj = temp;
				}
				this.Update = this.Update_Waiting;
				this.mc.scaleX = 2 * this.xw;
				this.mc.scaleY = 2 * this.yw;
				this.Draw();
				this.mc.visible = true;
			}
		}

		public function UnInit():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
			Main.getInstance().objects.EndUpdate(this);
			Main.getInstance().objects.EndDraw(this);
		}

		public function DumpInitData():String
		{
			var _local1 = (((("" + this.pos.x) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + this.pos.y) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + this.dirEnum;
			return(_local1);
		}

		public function IdleAfterDeath():void
		{
			if (this.isMoving)
			{
				this.Update_Waiting = this.Update_Idle;
			}
			else
			{
				this.Update = this.Update_Idle;
			}
		}

		public function Update_Idle():void
		{
		}

		public function Draw():void
		{
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
		}

		public function TestVsPlayer(guy):void
		{
			var _local2 = guy;
			var p = _local2.pos;
			var dy = (p.y - this.pos.y);
			var ady = Math.abs(dy);
			var _local3 = (this.yw + _local2.yw) - ady;
			if (0 < _local3)
			{
				var dx = (p.x - this.pos.x);
				var adx = Math.abs(dx);
				var px = ((this.xw + _local2.xw) - adx);
				if (0 < px)
				{
					if (_local3 < px)
					{
						if (dy < 0)
						{
							if (this.dir.y < 0)
							{
								Main.getInstance().particles.SpawnZapThwompV(this.pos, this.xw, -this.yw, _local2.pos);
								Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_ELECTRIC, 0, -8, _local2.pos.x, _local2.pos.y - (0.5 * _local2.r), this);
							}
							else
							{
								_local2.ReportCollisionVsObject(0, -_local3, 0, -1, this);
							}
						}
						else if (0 < this.dir.y)
						{
							Main.getInstance().particles.SpawnZapThwompV(this.pos, this.xw, this.yw, _local2.pos);
							Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_ELECTRIC, 0, 6, _local2.pos.x, _local2.pos.y + (0.5 * _local2.r), this);
						}
						else
						{
							_local2.ReportCollisionVsObject(0, _local3, 0, 1, this);
						}
					}
					else if (dx < 0)
					{
						if (this.dir.x < 0)
						{
							Main.getInstance().particles.SpawnZapThwompH(this.pos, -this.xw, this.yw, _local2.pos);
							Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_ELECTRIC, -8, -4, _local2.pos.x - (0.5 * _local2.r), _local2.pos.y, this);
						}
						else
						{
							_local2.ReportCollisionVsObject(-px, 0, -1, 0, this);
						}
					}
					else if (0 < this.dir.x)
					{
						Main.getInstance().particles.SpawnZapThwompH(this.pos, this.xw, this.yw, _local2.pos);
						Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_ELECTRIC, 8, -4, _local2.pos.x + (0.5 * _local2.r), _local2.pos.y, this);
					}
					else
					{
						_local2.ReportCollisionVsObject(px, 0, 1, 0, this);
					}
				}
			}
		}

		public function StartFall():void
		{
			this.isMoving = true;
			this.speed = this.fallspeed;
			this.movedir = 1;
			this.goal = this.fallgoal;
			this.Update = this.Update_Moving;
			Main.getInstance().objects.StartDraw(this);
		}

		public function StartRaise():void
		{
			this.isMoving = true;
			this.speed = this.raisespeed;
			this.movedir = -1;
			this.goal = this.anchor;
			this.Update = this.Update_Moving;
		}

		public function StartWait():void
		{
			this.isMoving = false;
			this.Update = this.Update_Waiting;
			Main.getInstance().objects.EndDraw(this);
		}

		public function Update_Waiting():void
		{
			if (this.dir.x == 0)
			{
				if (Math.abs(this.pos.x - PlayerObject.player.pos.x) < (2 * (this.xw + PlayerObject.player.xw)))
				{
					var _local2 = PlayerObject.player.cell.j;
					if ((this.maxj < _local2) || (_local2 < this.minj))
					{
					}
					else
					{
						this.StartFall();
					}
				}
			}
			else if (Math.abs(this.pos.y - PlayerObject.player.pos.y) < (2 * (this.yw + PlayerObject.player.yw)))
			{
				var _local2 = PlayerObject.player.cell.i;
				if ((this.maxi < _local2) || (_local2 < this.mini))
				{
				}
				else
				{
					this.StartFall();
				}
			}
		}

		public function Update_Moving():void
		{
			var _local3 = this.goal.x - this.pos.x;
			var _local2 = this.goal.y - this.pos.y;
			var len2 = ((_local3 * _local3) + (_local2 * _local2));
			if (len2 < (this.speed * this.speed))
			{
				this.pos.x = this.goal.x;
				this.pos.y = this.goal.y;
				if (this.movedir == 1)
				{
					this.StartRaise();
				}
				else
				{
					this.StartWait();
				}
			}
			else
			{
				this.pos.x = this.pos.x + ((this.movedir * this.dir.x) * this.speed);
				this.pos.y = this.pos.y + ((this.movedir * this.dir.y) * this.speed);
			}
			Main.getInstance().objects.Moved(this);
		}
	}
}